package top.anka1.ims;

import top.anka1.ims.RestaurantManagementSystem.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;

public class RestaurantGUI extends JFrame {
    private RestaurantService restaurantService = new RestaurantService();

    public void windowCenterAlignment(JFrame frame) {
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowx = (int) screensize.getWidth() / 2 - frame.getWidth() / 2;
        int windowy = (int) screensize.getHeight() / 2 - frame.getHeight() / 2;
        frame.setLocation(windowx, windowy);
    }

    public RestaurantGUI() {
        restaurantService.addSampleIngredients();
    }

    public RestaurantService getRestaurantService() {
        return restaurantService;
    }

    public void ManageIngredients() {
        JFrame manageIngredientsFrame = new JFrame("Manage Ingredients");
        manageIngredientsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        manageIngredientsFrame.setSize(400, 300);
        windowCenterAlignment(manageIngredientsFrame);
        manageIngredientsFrame.setLayout(new BorderLayout());

        JLabel title = new JLabel("===== INGREDIENT MANAGEMENT =====", SwingConstants.CENTER);
        title.setFont(new Font("Arial", Font.PLAIN, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        manageIngredientsFrame.add(title, BorderLayout.NORTH);

        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(0, 1, 5, 5));
        JButton addIngredientButton = new JButton("Add New Ingredient");
        addIngredientButton.addActionListener(e -> {AddNewIngredient();});
        buttonGridPanel.add(addIngredientButton);

        JButton viewIngredientsButton = new JButton("View All Ingredients");
        viewIngredientsButton.addActionListener(e -> {viewAllIngredients();});
        buttonGridPanel.add(viewIngredientsButton);

        JButton removeIngredientButton = new JButton("Remove Ingredient");
        removeIngredientButton.addActionListener(e -> {removeIngredient();});
        buttonGridPanel.add(removeIngredientButton);

        JButton backButton = new JButton("Back to Main Menu");
        backButton.addActionListener(e -> {manageIngredientsFrame.dispose();});
        buttonGridPanel.add(backButton);

        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);
        manageIngredientsFrame.getContentPane().add(wrapper, BorderLayout.CENTER);
        manageIngredientsFrame.setVisible(true);

    }
    public void AddNewIngredient() {
        JFrame addIngredientFrame = new JFrame("Add New Ingredient");
        addIngredientFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addIngredientFrame.setSize(600, 100);
        windowCenterAlignment(addIngredientFrame);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Ingredient"));

        JLabel nameLabel = new JLabel("Ingredient Name:");
        JTextField nameField = new JTextField(10);
        JLabel priceLabel = new JLabel("Price:");
        JTextField priceField = new JTextField(10);

        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            String name = nameField.getText();
            double price = Double.parseDouble(priceField.getText());
            restaurantService.addIngredient(name, price);
            JOptionPane.showMessageDialog(addIngredientFrame, "Ingredient added successfully!");
            //addIngredientFrame.dispose();
        });

        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(priceLabel);
        inputPanel.add(priceField);
        inputPanel.add(submitButton);
        addIngredientFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);

        addIngredientFrame.setVisible(true);
    }
    public void viewAllIngredients() {
        JFrame viewIngredientsFrame = new JFrame("View All Ingredients");
        viewIngredientsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        viewIngredientsFrame.setSize(400, 300);
        windowCenterAlignment(viewIngredientsFrame);

        JScrollPane scrollPane = getAllIngredientsJScrollPane();
        viewIngredientsFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        viewIngredientsFrame.setVisible(true);
    }
    public JScrollPane getAllIngredientsJScrollPane() {
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        if (restaurantService.getIngredientInventory().isEmpty()) {
            textArea.append("No ingredients available.");
        } else {
            textArea.append("===== INGREDIENT INVENTORY =====\n");
            for (String name : restaurantService.getIngredientInventory().keySet()) {
                Ingredient ingredient = restaurantService.getIngredientInventory().get(name);
                textArea.append(ingredient.toString() + "\n");
            }
            textArea.append("==============================");
        }
        return new JScrollPane(textArea);
    }
    public String getAllIngredientsText() {
        String AllIngredientsText = "";
        if (restaurantService.getIngredientInventory().isEmpty()) {
            AllIngredientsText = "No ingredients available.";
        } else {
            AllIngredientsText = "===== INGREDIENT INVENTORY =====\n";
            for (String name : restaurantService.getIngredientInventory().keySet()) {
                Ingredient ingredient = restaurantService.getIngredientInventory().get(name);
                AllIngredientsText += ingredient.toString() + "\n";
            }
            AllIngredientsText += "==============================";
        }
        return AllIngredientsText;
    }
    public void removeIngredient() {
        if (restaurantService.getIngredientInventory().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No ingredients available to remove.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame removeIngredientFrame = new JFrame("Remove Ingredient");
        removeIngredientFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        removeIngredientFrame.setSize(400, 400);
        windowCenterAlignment(removeIngredientFrame);

        JTextArea allIngredientsTextArea = new JTextArea(getAllIngredientsText());
        allIngredientsTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(allIngredientsTextArea);
        removeIngredientFrame.getContentPane().add(scrollPane, BorderLayout.NORTH);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Remove Ingredient"));

        JLabel nameLabel = new JLabel("Enter the name of the ingredient to remove: ");
        JTextField nameField = new JTextField(10);
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            String name = nameField.getText();
            if (!restaurantService.getIngredientInventory().containsKey(name)) {
                JOptionPane.showMessageDialog(removeIngredientFrame, "Ingredient '" + name + "' not found!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            restaurantService.getIngredientInventory().remove(name);
            JOptionPane.showMessageDialog(removeIngredientFrame, "Ingredient '" + name + "' removed successfully!");
            allIngredientsTextArea.setText(getAllIngredientsText());
            //removeIngredientFrame.dispose();
        });

        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(submitButton);
        removeIngredientFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);

        removeIngredientFrame.setVisible(true);
    }

    public void manageMeals() {
        JFrame manageMealsFrame = new JFrame("Manage Meals");
        manageMealsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        manageMealsFrame.setSize(400, 300);
        windowCenterAlignment(manageMealsFrame);
        manageMealsFrame.setLayout(new BorderLayout());

        JLabel title = new JLabel("===== MEAL MANAGEMENT =====", SwingConstants.CENTER);
        title.setFont(new Font("Arial", Font.PLAIN, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        manageMealsFrame.add(title, BorderLayout.NORTH);

        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(0, 1, 5, 5));
        JButton createNewMealButton = new JButton("Create New Meal");
        createNewMealButton.addActionListener(e -> {createNewMeal();});
        buttonGridPanel.add(createNewMealButton);

        JButton removeMealButton = new JButton("Remove Meal");
        removeMealButton.addActionListener(e -> {removeMeal();});
        buttonGridPanel.add(removeMealButton);

        JButton viewMealsButton = new JButton("View All Created Meals");
        viewMealsButton.addActionListener(e -> {viewAllMeals();});
        buttonGridPanel.add(viewMealsButton);

        JButton addMealToMenuButton = new JButton("Add Meal to Menu");
        addMealToMenuButton.addActionListener(e -> {addMealToMenu();});
        buttonGridPanel.add(addMealToMenuButton);

        JButton removeMealFromMenuButton = new JButton("Remove Meal from Menu");
        removeMealFromMenuButton.addActionListener(e -> {removeMealFromMenu();});
        buttonGridPanel.add(removeMealFromMenuButton);

        JButton backButton = new JButton("Back to Main Menu");
        backButton.addActionListener(e -> {manageMealsFrame.dispose();});
        buttonGridPanel.add(backButton);

        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);
        manageMealsFrame.getContentPane().add(wrapper, BorderLayout.CENTER);
        manageMealsFrame.setVisible(true);
    }
    public void createNewMeal() {
        if (restaurantService.getIngredientInventory().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No ingredients available. Please add ingredients first.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame createMealFrame = new JFrame("Create New Meal");
        createMealFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        createMealFrame.setSize(600, 350);
        windowCenterAlignment(createMealFrame);
        JScrollPane scrollPane = getAllIngredientsJScrollPane();
        createMealFrame.getContentPane().add(scrollPane, BorderLayout.NORTH);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Meal"));

        JLabel nameLabel = new JLabel("Meal Name:");
        JTextField nameField = new JTextField(10);
        JLabel ingredientsLabel = new JLabel("Ingredients (comma-separated):");
        JTextField ingredientsField = new JTextField(10);

        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            String name = nameField.getText();
            String[] ingredients = ingredientsField.getText().split(",");
            if (restaurantService.createMeal(name, new ArrayList<>(Arrays.asList(ingredients)), createMealFrame)) {
            //createMealFrame.dispose();
            }
        });

        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(ingredientsLabel);
        inputPanel.add(ingredientsField);
        inputPanel.add(submitButton);
        createMealFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);

        createMealFrame.setVisible(true);
    }
    public void viewAllMeals() {
        if (restaurantService.getMeals().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No meals available. Please create meals first.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame viewMealsFrame = new JFrame("View All Meals");
        viewMealsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        viewMealsFrame.setSize(400, 300);
        windowCenterAlignment(viewMealsFrame);

        JScrollPane scrollPane = getAllMealsJScrollPane();
        viewMealsFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        viewMealsFrame.setVisible(true);
    }
    public JScrollPane getAllMealsJScrollPane() {
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        if (restaurantService.getMeals().isEmpty()) {
            textArea.append("No meals available.");
        } else {
            textArea.append("===== CREATED MEALS =====\n");
            for (int i = 0; i < restaurantService.getMeals().size(); i++) {
                textArea.append((i + 1) + ". " + restaurantService.getMeals().get(i).toString() + "\n");
            }
            textArea.append("==============================");
        }
        return new JScrollPane(textArea);
    }
    public String getAllMealsText() {
        String AllMealsText = "";
        if (restaurantService.getMeals().isEmpty()) {
            AllMealsText = "No meals available.";
        } else {
            AllMealsText = "===== CREATED MEALS =====\n";
            for (int i = 0; i < restaurantService.getMeals().size(); i++) {
                AllMealsText += (i + 1) + ". " + restaurantService.getMeals().get(i).toString() + "\n";
            }
            AllMealsText += "==============================";
        }
        return AllMealsText;
    }
    public void removeMeal() {
        if (restaurantService.getMeals().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No meals available to remove.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame removeMealFrame = new JFrame("Remove Meal");
        removeMealFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        removeMealFrame.setSize(400, 300);
        windowCenterAlignment(removeMealFrame);

        JTextArea allMealsTextArea = new JTextArea(getAllMealsText());
        allMealsTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(allMealsTextArea);
        removeMealFrame.getContentPane().add(scrollPane, BorderLayout.NORTH);

        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Remove Meal"));
        JLabel choiceLabel = new JLabel("Enter the number of the meal to remove: ");
        JTextField choiceField = new JTextField(10);
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            int choice;
            try {
                choice = Integer.parseInt(choiceField.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(removeMealFrame, "Invalid input! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (choice < 1 || choice > restaurantService.getMeals().size()) {
                JOptionPane.showMessageDialog(removeMealFrame, "Invalid choice! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Meal selectedMeal = restaurantService.getMeals().get(choice - 1);
            if (restaurantService.removeMeal(selectedMeal.getName(), removeMealFrame)) {
                allMealsTextArea.setText(getAllMealsText());
                // removeMealFrame.dispose();
            }
        });

        inputPanel.add(choiceLabel);
        inputPanel.add(choiceField);
        inputPanel.add(submitButton);
        removeMealFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);
        removeMealFrame.setVisible(true);

    }
    public void addMealToMenu() {
        if (restaurantService.getMeals().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No meals have been created yet to add to the menu.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame addMealToMenuFrame = new JFrame("Add Meal to Menu");
        addMealToMenuFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addMealToMenuFrame.setSize(400, 300);
        windowCenterAlignment(addMealToMenuFrame);
        JScrollPane AllMeals = getAllMealsJScrollPane();
        addMealToMenuFrame.getContentPane().add(AllMeals, BorderLayout.NORTH);

        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Add Meal to Menu"));
        JLabel choiceLabel = new JLabel("Enter the number of the meal to add to the menu: ");
        JTextField choiceField = new JTextField(10);
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            int choice;
            try {
                choice = Integer.parseInt(choiceField.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(addMealToMenuFrame, "Invalid input! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (choice < 1 || choice > restaurantService.getMeals().size()) {
                JOptionPane.showMessageDialog(addMealToMenuFrame, "Invalid choice! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Meal selectedMeal = restaurantService.getMeals().get(choice - 1);
            restaurantService.addMealToMenu(selectedMeal);
            JOptionPane.showMessageDialog(addMealToMenuFrame, "Meal '" + selectedMeal.getName() + "' added to the menu successfully!");
            //addMealToMenuFrame.dispose();
        });
        JButton finishOrderingButton = new JButton("Finish Ordering");
        finishOrderingButton.addActionListener(e -> {restaurantService.finishOrdering(addMealToMenuFrame);});
        inputPanel.add(choiceLabel);
        inputPanel.add(choiceField);
        inputPanel.add(submitButton);
        inputPanel.add(finishOrderingButton);
        addMealToMenuFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);

        addMealToMenuFrame.setVisible(true);
    }
    public JScrollPane getMenuJScrollPane() {
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        if (restaurantService.getMeals().isEmpty()) {
            textArea.append("No meals available.");
        } else {
            textArea.append("===== MENU =====\n");
            for (int i = 0; i < restaurantService.getMenu().size(); i++) {
                textArea.append((i + 1) + ". " + restaurantService.getMenu().get(i).toString() + "\n");
            }
            textArea.append("==============================");
        }
        return new JScrollPane(textArea);
    }
    public String getMenuText() {
        String menuText = "";
        if (restaurantService.getMenu().isEmpty()) {
            menuText = "No meals available.";
        } else {
            menuText = "===== MENU =====\n";
            for (int i = 0; i < restaurantService.getMenu().size(); i++) {
                menuText += (i + 1) + ". " + restaurantService.getMenu().get(i).toString() + "\n";
            }
            menuText += "==============================";
        }
        return menuText;
    }
    public void removeMealFromMenu() {
        if (restaurantService.getMenu().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No meals available to remove from the menu.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFrame removeMealFrame = new JFrame("Remove Meal from Menu");
        removeMealFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        removeMealFrame.setSize(400, 300);
        windowCenterAlignment(removeMealFrame);

        JTextArea allMealsTextArea = new JTextArea(getMenuText());
        allMealsTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(allMealsTextArea);
        removeMealFrame.getContentPane().add(scrollPane, BorderLayout.NORTH);

        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Remove Meal from Menu"));
        JLabel choiceLabel = new JLabel("Enter the number of the meal to remove from the menu: ");
        JTextField choiceField = new JTextField(10);
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(e -> {
            int choice;
            try {
                choice = Integer.parseInt(choiceField.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(removeMealFrame, "Invalid input! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (choice < 1 || choice > restaurantService.getMeals().size()) {
                JOptionPane.showMessageDialog(removeMealFrame, "Invalid choice! Please enter a valid meal number.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Meal selectedMeal = restaurantService.getMeals().get(choice - 1);
            restaurantService.removeMealFromMenu(selectedMeal);
            JOptionPane.showMessageDialog(removeMealFrame, "Meal '" + selectedMeal.getName() + "' removed from the menu successfully!");
            allMealsTextArea.setText(getMenuText());
            //removeMealFrame.dispose();
        });
        JButton finishOrderingButton = new JButton("Finish Ordering");
        finishOrderingButton.addActionListener(e -> {restaurantService.finishOrdering(removeMealFrame);});

        inputPanel.add(choiceLabel);
        inputPanel.add(choiceField);
        inputPanel.add(submitButton);
        inputPanel.add(finishOrderingButton);
        removeMealFrame.getContentPane().add(inputPanel, BorderLayout.CENTER);

        removeMealFrame.setVisible(true);
    }

    public void manageMenu() {
        JFrame manageMenuFrame = new JFrame("Manage Menu");
        manageMenuFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        manageMenuFrame.setSize(400, 300);
        windowCenterAlignment(manageMenuFrame);
        manageMenuFrame.setLayout(new BorderLayout());

        JLabel title = new JLabel("===== MENU MANAGEMENT =====", SwingConstants.CENTER);
        title.setFont(new Font("Arial", Font.PLAIN, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        manageMenuFrame.add(title, BorderLayout.NORTH);

        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(0, 1, 5, 5));
        JButton viewMenuButton = new JButton("View Menu");
        viewMenuButton.addActionListener(e -> {viewMenu();});
        buttonGridPanel.add(viewMenuButton);

        JButton removeMealFromMenuButton = new JButton("Remove Meal from Menu");
        removeMealFromMenuButton.addActionListener(e -> {removeMealFromMenu();});
        buttonGridPanel.add(removeMealFromMenuButton);

        JButton backButton = new JButton("Back to Main Menu");
        backButton.addActionListener(e -> {manageMenuFrame.dispose();});
        buttonGridPanel.add(backButton);

        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);
        manageMenuFrame.getContentPane().add(wrapper, BorderLayout.CENTER);
        manageMenuFrame.setVisible(true);
    }
    public void viewMenu() {
        JFrame viewMenuFrame = new JFrame("View Menu");
        viewMenuFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        viewMenuFrame.setSize(400, 300);
        windowCenterAlignment(viewMenuFrame);

        JScrollPane scrollPane = getMenuJScrollPane();
        viewMenuFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        viewMenuFrame.setVisible(true);
    }
    public void manageOrders() {
        JFrame manageOrdersFrame = new JFrame("Manage Orders");
        manageOrdersFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        manageOrdersFrame.setSize(400, 300);
        windowCenterAlignment(manageOrdersFrame);
        manageOrdersFrame.setLayout(new BorderLayout());

        JLabel title = new JLabel("===== ORDER MANAGEMENT =====", SwingConstants.CENTER);
        title.setFont(new Font("Arial", Font.PLAIN, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        manageOrdersFrame.add(title, BorderLayout.NORTH);

        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(0, 1, 5, 5));
        JButton viewMenuButton = new JButton("View Menu");
        viewMenuButton.addActionListener(e -> {viewMenu();});
        buttonGridPanel.add(viewMenuButton);

        JButton addMealToOrderButton = new JButton("Add Meal to Order");
        addMealToOrderButton.addActionListener(e -> {addMealToMenu();});
        buttonGridPanel.add(addMealToOrderButton);

        JButton viewCurrentOrderButton = new JButton("View Current Order");
        viewCurrentOrderButton.addActionListener(e -> {viewMenu();});
        buttonGridPanel.add(viewCurrentOrderButton);

        JButton generateBillButton = new JButton("Generate Bill");
        generateBillButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(manageOrdersFrame,restaurantService.getOrderById(restaurantService.finishOrdering(manageOrdersFrame)).generateBill(JOptionPane.showInputDialog("Enter customer name: ")));
        });
        buttonGridPanel.add(generateBillButton);

        JButton clearOrderButton = new JButton("Clear Order");
        clearOrderButton.addActionListener(e -> {restaurantService.getOrderById(restaurantService.finishOrdering(manageOrdersFrame)).clearOrder();});

        JButton backButton = new JButton("Back to Main Menu");
        backButton.addActionListener(e -> {manageOrdersFrame.dispose();});
        buttonGridPanel.add(backButton);

        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);
        manageOrdersFrame.getContentPane().add(wrapper, BorderLayout.CENTER);
        manageOrdersFrame.setVisible(true);
    }
    public void MainGUI() {
        JFrame mainFrame = new JFrame("Restaurant Management System");
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setSize(800, 500);
        windowCenterAlignment(mainFrame);
        mainFrame.setLayout(new BorderLayout());

        JLabel headLine = new JLabel("===== RESTAURANT MANAGEMENT SYSTEM =====", SwingConstants.CENTER);
        headLine.setFont(new Font("Arial", Font.BOLD, 24));
        headLine.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
        mainFrame.add(headLine, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 10, 10));

        JButton ManageIngredientsButton = new JButton("Manage Ingredients");
        ManageIngredientsButton.addActionListener(e -> {ManageIngredients();});
        panel.add(ManageIngredientsButton);

        JButton ManageMealsButton = new JButton("Manage Meals");
        ManageMealsButton.addActionListener(e -> {manageMeals();});
        panel.add(ManageMealsButton);

        JButton ManageMenuButton = new JButton("Manage Menu");
        ManageMenuButton.addActionListener(e -> {manageMenu();});
        panel.add(ManageMenuButton);

        JButton ManageOrderButton = new JButton("Manage Orders");
        ManageOrderButton.addActionListener(e -> {manageOrders();});
        panel.add(ManageOrderButton);

        JButton aboutButton = new JButton("About");
        aboutButton.addActionListener(e -> {
            SwingUtilities.invokeLater(About_Copyright::new);
        });
        panel.add(aboutButton);

        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                //System.exit(0);
                mainFrame.dispose();
            }
        });
        panel.add(exitButton);

        // 包装一个带边距的外层容器
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50));
        wrapper.add(panel, BorderLayout.CENTER);

        mainFrame.getContentPane().add(wrapper, BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            RestaurantGUI restaurantGUI = new RestaurantGUI();
            restaurantGUI.MainGUI();
        });
    }
}
